package org.example.contractFinance.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import org.example.account.domain.Account;
import org.example.account.mapper.AccountMapper;
import org.example.utils.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.example.contractFinance.mapper.ContractFinanceMapper;
import org.example.contractFinance.domain.ContractFinance;
import org.example.contractFinance.service.IContractFinanceService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 合同财务Service业务层处理
 *
 * @author 小孟微信：xiaomengcode
 * @date 2024-04-20
 */
@Service
public class ContractFinanceServiceImpl implements IContractFinanceService
{
    @Autowired
    private ContractFinanceMapper contractFinanceMapper;

    @Autowired
    private AccountMapper accountMapper;


    /**
     * 查询合同财务
     *
     * @param financeId 合同财务主键
     * @return 合同财务
     */
    @Override
    public ContractFinance selectContractFinanceByFinanceId(Long financeId)
    {
        return contractFinanceMapper.selectContractFinanceByFinanceId(financeId);
    }

    /**
     * 查询合同财务列表
     *
     * @param contractFinance 合同财务
     * @return 合同财务
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<ContractFinance> selectContractFinanceList(ContractFinance contractFinance)
    {
        Util.setQuerySelfData((BaseEntity) contractFinance);
        List<ContractFinance> list=contractFinanceMapper.selectContractFinanceList(contractFinance);
        return list;
    }

    /**
     * 新增合同财务
     *
     * @param contractFinance 合同财务
     * @return 结果
     */
    @Override
    @Transactional
    public int insertContractFinance(ContractFinance contractFinance)
    {
        Account account = accountMapper.selectAccountByAccountId(Long.parseLong(contractFinance.getAccountId().toString()));
        account.setUseMoney(account.getUseMoney().add(contractFinance.getTotalMoney()));
        //判断是否超出
        if(account.getRemainMoney().compareTo(contractFinance.getTotalMoney())<0){
           throw new ServiceException("超出账单总金额:"+account.getRemainMoney().subtract(contractFinance.getTotalMoney()));
        }else{
            account.setRemainMoney(account.getRemainMoney().subtract(contractFinance.getTotalMoney()));
        }
        accountMapper.updateAccount(account);

        contractFinance.setCreateBy(ShiroUtils.getUserId()+"");
        contractFinance.setCreateTime(DateUtils.getNowDate());
        return contractFinanceMapper.insertContractFinance(contractFinance);
    }

    /**
     * 修改合同财务
     *
     * @param contractFinance 合同财务
     * @return 结果
     */
    @Override
    public int updateContractFinance(ContractFinance contractFinance)
    {
        return contractFinanceMapper.updateContractFinance(contractFinance);
    }

    /**
     * 批量删除合同财务
     *
     * @param financeIds 需要删除的合同财务主键
     * @return 结果
     */
    @Override
    public int deleteContractFinanceByFinanceIds(String financeIds)
    {
        return contractFinanceMapper.deleteContractFinanceByFinanceIds(Convert.toStrArray(financeIds));
    }

    /**
     * 删除合同财务信息
     *
     * @param financeId 合同财务主键
     * @return 结果
     */
    @Override
    public int deleteContractFinanceByFinanceId(Long financeId)
    {
        return contractFinanceMapper.deleteContractFinanceByFinanceId(financeId);
    }
}
